import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:get/get.dart';

import '../../../event/DataCallBack.dart';
import '../../../nets/NetWorkManager.dart';
import '../../../routes/app_pages.dart';

import 'package:smart_translator/app/utils/CommonUtil.dart';

import '../../../utils/tips.dart';


class ForgotPasswdController extends GetxController {

  var suffixIconsVisible = false.obs;
  var pwVisible = false.obs;
  var rePwVisible = false.obs;
  var accountController = TextEditingController().obs;
  var passwordCodeController = TextEditingController().obs;
  var rePasswordCodeController = TextEditingController().obs;

  var isSubmitBtnEnable = false.obs;
  var checkBoxChecked = false.obs;

  late StreamSubscription<bool> keyboardSubscription;
  var scrollController = ScrollController().obs;

  @override
  void onInit() {
    super.onInit();
    accountController.value.text='';
    passwordCodeController.value.text='';
    rePasswordCodeController.value.text='';
    isSubmitBtnEnable.value = false;
    keyboardSubscription = KeyboardVisibilityController().onChange.listen((bool visible) {
      print('Keyboard visibility update. Is visible: $visible');
      if(visible) {
        //scrollToBottom();
        Future.delayed(Duration(milliseconds: 500), () {
          scrollToBottom();
        });

      }
    });

  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
    scrollController.value.dispose();
    keyboardSubscription.cancel();
  }


  void setSuffixIconsVisible(bool visible) {
    suffixIconsVisible.value = visible;
  }

  void onTextFieldChange(v) {
    if (v != null && v != '') {
      suffixIconsVisible.value = true;
    } else {
      suffixIconsVisible.value = false;
    }

    //检查btn提交按钮是否可以点击.
    if(simpleChecking()) {
      isSubmitBtnEnable.value = true;
    } else {
      isSubmitBtnEnable.value = false;
    }
  }

  void onPwFieldChange(v) {
    //检查btn提交按钮是否可以点击.
    if(simpleChecking()) {
      isSubmitBtnEnable.value = true;
    } else {
      isSubmitBtnEnable.value = false;
    }
  }

  bool simpleChecking() {
    return accountController.value.text != '' && passwordCodeController.value.text != '' && rePasswordCodeController.value.text != '';
  }

  void onCheckBoxChange() {
    if(simpleChecking()) {
      isSubmitBtnEnable.value = true;
    } else {
      isSubmitBtnEnable.value = false;
    }
  }

  void checkValidateCode() {
    var isEmialType =  CommonUtil.isEmail(accountController.value.text);
    if(isEmialType){
      getEmailValidateCode(accountController.value.text);
    }else{
      getPhoneValidateCode(accountController.value.text);
    }
  }

  void scrollToBottom() {
    scrollController.value.animateTo(
      scrollController.value.position.maxScrollExtent,
      duration: const Duration(seconds: 2),
      curve: Curves.easeOut,
    );
  }

  // 获取手机验证码
  void getPhoneValidateCode(text) async{
    NetWorkManager.getInstance().requestBussiness(NetWorkManager.SEND_PHONE_VERIFICATION_CODE, {
      'phone': text,
      'smsmode': 2

    }, NetWorkManager.methodPOST, DataCallBack(
        businessCallBack: (v) {
          Get.toNamed(Routes.FORGET_VALIDATE_CODE, arguments: {
            'account':text,
            'passwd': passwordCodeController.value.text
          });
          clearData();
        },
        errorCallBack: (v) {
          print('error $v');
          Tips.showToast(v);
        }

    ),showLoading: true);
  }

//   获取邮箱验证码
void getEmailValidateCode(text) async{
  NetWorkManager.getInstance().requestBussiness(NetWorkManager.SEND_EMAIL_VERIFICATION_CODE, {
    'email': text,
    'codemode': 2

  }, NetWorkManager.methodPOST, DataCallBack(
      businessCallBack: (v) {
        Get.toNamed(Routes.FORGET_VALIDATE_CODE, arguments: {
          'account':text,
          'passwd': passwordCodeController.value.text
        });
        clearData();
      },
      errorCallBack: (v) {
        Tips.showToast(v);
      }

  ),showLoading: true);
}

// 跳转到下个页面前清除数据
void clearData(){
  suffixIconsVisible.value = false;
  pwVisible.value = false;
  rePwVisible.value=false;
  accountController.value.text='';
  passwordCodeController.value.text='';
  rePasswordCodeController.value.text='';
  isSubmitBtnEnable.value = false;
  checkBoxChecked.value=false;
}




}
